<?php
declare (strict_types=1);

namespace app\listener;

use app\common\model\Trade;
use app\index\service\TradableInterface;

class RefundSuccess
{
    /**
     * 退款审核通过
     * @param Trade $trade
     * @return void
     */
    public function handle(Trade $trade)
    {
        // 退款转账已完成，完成后续业务逻辑
        $service = $this->getTradeService($trade->tradeTypeClass);
        if (is_null($service)) {
            // TODO 记录错误日志
            return;
        }
        $service->completionRefundTrade($trade);
    }

    /**
     * 获取对应业务逻辑Service
     * @param $tradeTypeClass
     * @return TradableInterface|null
     */
    private function getTradeService($tradeTypeClass): ?TradableInterface
    {
        return app_instance(TradableInterface::class, $tradeTypeClass);
    }
}
